import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.LinkedList;
import java.util.Scanner;

import walter.WSolver;

import Algoritmos.Simulator;

import com.google.gson.Gson;

import estructuras.common.PaqueteBackend;
import estructuras.common.RutaDeVueloBackend;


public class RequestHandler implements Runnable {
	private Socket socket;
	public static final String PAQUETES_PREFIJO = "paquetes:";
	public static final String GENERA_RUTAS = "genera_rutas:";
	public static final String SELECCIONA = "selecciona:";
	
	public RequestHandler(Socket socket){
		this.socket = socket;
	}
	@Override
	public void run() {
		try {
			
			String command = getComandoFromCliente();
			if(command != null){
				/*OutputStream os = socket.getOutputStream();
				PrintWriter pw = new PrintWriter(os, true);
				Mensaje m = new Mensaje();
				m.mensaje = getResultFromSimulator();
				Gson gson = new Gson();
				pw.println(gson.toJson(m));
				pw.flush();
				pw.close();	
				*/			
				if(command.startsWith(PAQUETES_PREFIJO)){
					gestionaComandosPaquetes(command.substring(0, PAQUETES_PREFIJO.length()));
				}
			}
			socket.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
	}
	
	private void gestionaComandosPaquetes(String command) throws IOException {
		comienzaSiMuerto();
		Gson gson = new Gson();
		InputStream is = socket.getInputStream();
		Scanner in = new Scanner(is);
		OutputStream os = socket.getOutputStream();
		PrintWriter pw = new PrintWriter(os, true);
		
		if(command.startsWith(GENERA_RUTAS)){
			//lee paquete
			String paquetejson = command.substring(GENERA_RUTAS.length());
			PaqueteBackend paqueteBackend = gson.fromJson(paquetejson, PaqueteBackend.class);
			//devuelve rutas
			RutaDeVueloBackend[] result = new RutaDeVueloBackend[0];
			pw.println(gson.toJson(result)); pw.flush();
		}else if(command.startsWith(SELECCIONA)){
			//selecciona ruta y devuelve true si pudo usarla
			String rutajson = command.substring(SELECCIONA.length());
			RutaDeVueloBackend ruta = gson.fromJson(rutajson, RutaDeVueloBackend.class);
			pw.print(false); pw.flush();
		}
		
		in.close();
		pw.close();		
	}
	private void comienzaSiMuerto() {
		// TODO Auto-generated method stub
		
	}
	private String getComandoFromCliente() throws IOException{
		InputStream is = socket.getInputStream();
		Scanner in = new Scanner(is);
		return in.next();
	}
	/*private String getResultFromSimulator() {
		Simulator simulator = new Simulator(new WSolver());

		
			InputStream inputStream;
			long start = System.currentTimeMillis();
			try {
				//inputStream =System.in;
				inputStream = (new FileInputStream( new File( "input.txt" ) ) );
				simulator.run( inputStream );
				inputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			long end = System.currentTimeMillis();
			return "tiempo en segs.: " + (end - start) / 1000.0;
		
	}*/

}
